package cn.plushine.controller;


import cn.plushine.pojo.Customer;
import cn.plushine.service.BaseCustomerService;
import cn.plushine.utils.ResMapUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@Api(tags = "客户信息")
@RestController
@RequestMapping("/admin/product/customer")
public class BaseCustomerController {

    @Autowired
    BaseCustomerService baseCustomerService;

    // 获取客户信息
    @ApiOperation(value = "获取客户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "input", value = "关键字", paramType = "query", dataType = "String")
    })
    @GetMapping("/get")
    public HashMap<String, Object> delete(String input) {
        try {
            // 获取客户信息
            List<Customer> customerListDB = baseCustomerService.get(input);
            // 成功响应
            return ResMapUtils.encapsulation(200, "获取客户信息成功！", customerListDB);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "获取客户信息失败！", null);
        }
    }

    // 添加客户信息
    @ApiOperation(value = "添加客户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customer", value = "客户信息", required = true, paramType = "body", dataType = "Customer")
    })
    @PostMapping("/add")
    public HashMap<String, Object> add(@RequestBody Customer customer) {
        try {
            // 添加客户信息
            int addDB = baseCustomerService.add(customer);
            // 成功响应
            return ResMapUtils.encapsulation(200, "添加客户信息成功！", addDB);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "添加客户信息失败！", null);
        }
    }

    // 修改客户信息
    @ApiOperation(value = "修改客户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "customer", value = "客户信息", required = true, paramType = "body", dataType = "Customer")
    })
    @PutMapping("/modify")
    public HashMap<String, Object> modify(@RequestBody Customer customer) {
        try {
            // 修改客户信息
            baseCustomerService.modify(customer);
            // 成功响应
            return ResMapUtils.encapsulation(200, "修改客户信息成功！", null);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "修改客户信息失败！", e.getMessage());
        }
    }

    // 删除客户信息
    @ApiOperation(value = "删除客户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "客户id", example = "1", required = true, paramType = "path", dataType = "int")
    })
    @DeleteMapping("/delete/{id}")
    public HashMap<String, Object> delete(@PathVariable int id) {
        try {
            // 修改客户信息
            baseCustomerService.delete(id);
            // 成功响应
            return ResMapUtils.encapsulation(200, "删除客户信息成功！", null);
        } catch (Exception e) {
            // 失败响应
            return ResMapUtils.encapsulation(201, "删除客户信息失败！", e.getMessage());
        }
    }
}
